<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期名" prop="semeName">
        <el-input
          v-model="queryParams.semeName"
          placeholder="请输入学期名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="培训对象" prop="trainObject">
        <el-select v-model="queryParams.trainObject" placeholder="请选择培训对象" clearable size="small">
          <el-option
            v-for="dict in dict.type.ps_train_object"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['psedu-base:semester:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['psedu-base:semester:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['psedu-base:semester:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['psedu-base:semester:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="semesterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学期ID" align="center" prop="semeId" />
      <el-table-column label="学期名" align="center" prop="semeName" />
      <el-table-column label="培训对象" align="center" prop="trainObject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ps_train_object" :value="scope.row.trainObject"/>
        </template>
      </el-table-column>
      <el-table-column label="启动报名" align="center" prop="active">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.active"
            :inactive-value="0"
            :active-value="1"
            :disabled="scope.row.loadChangeActive"
            @change="changeActive(scope.row, $event, scope.$index)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="允许评教" align="center" prop="allowEvaluate" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.allowEvaluate"
            :inactive-value="0"
            :active-value="1"
            :disabled="scope.row.loadChangeAllowEvaluate"
            @change="changeAllowEvaluate(scope.row, $event, scope.$index)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="证书建议" align="center" prop="certificateComment" />
      <el-table-column label="党校建议" align="center" prop="partyComment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCourseList(scope.row)"
            v-hasPermi="['psedu-base:course:list']"
          >课程</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['psedu-base:semester:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['psedu-base:semester:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学期对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学期名" prop="semeName">
          <el-input v-model="form.semeName" placeholder="请输入学期名" />
        </el-form-item>
        <el-form-item label="培训对象" prop="trainObject">
          <el-select v-model="form.trainObject" placeholder="请选择培训对象">
            <el-option
              v-for="dict in dict.type.ps_train_object"
              :key="dict.value"
              :label="dict.label"
:value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="启动报名" prop="active">
          <el-switch
            v-model="form.active"
            :inactive-value="0"
            :active-value="1"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="允许评教" prop="allowEvaluate">
          <el-switch
            v-model="form.allowEvaluate"
            :inactive-value="0"
            :active-value="1"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="证书建议" prop="certificateComment">
          <el-input v-model="form.certificateComment" placeholder="请输入证书建议" />
        </el-form-item>
        <el-form-item label="党校建议" prop="partyComment">
          <el-input v-model="form.partyComment" placeholder="请输入党校建议" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSemester, getSemester, delSemester, addSemester, updateSemester, updateSemesterActive, updateSemesterAllowEvaluate  } from "@/api/psedu-base/semester";

export default {
  name: "Semester",
  dicts: ['ps_train_object'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学期表格数据
      semesterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semeName: null,
        trainObject: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        semeName: [
          { required: true, message: "学期名不能为空", trigger: "blur" }
        ],
        trainObject: [
          { required: true, message: "培训对象不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学期列表 */
    getList() {
      this.loading = true;
      listSemester(this.queryParams).then(response => {
        this.semesterList = response.rows;
        for(let i = 0; i < this.semesterList.length; i++) {
          let item = this.semesterList[i];
          item.loadChangeActive = false;
          item.loadChangeAllowEvaluate = false;
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCourseList(row) {
      this.$router.push({ path: "/train/course", query: {semeId: row.semeId}});
    },
    changeActive(row, $event, index) {
      row.loadChangeActive = true;
      this.$set(this.semesterList, index, row);
      let data = {
        semeId: row.semeId,
        active: $event
      }
      updateSemesterActive(data)
        .then((resp) => {
          row.loadChangeActive = false;
          this.$set(this.semesterList, index, row);
        })
    },
    /** 改变学期评教 */
    changeAllowEvaluate(row, $event, index) {
        row.loadChangeAllowEvaluate = true;
        this.$set(this.semesterList, index, row);
        let data = {
          semeId: row.semeId,
          allowEvaluate: $event
        }
        updateSemesterAllowEvaluate(data)
          .then((resp) => {
            row.loadChangeAllowEvaluate = false;
            this.$set(this.semesterList, index, row);
          })
      },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        semeId: null,
        semeName: null,
        trainObject: null,
        active: null,
        allowEvaluate: null,
        startTime: null,
        endTime: null,
        certificateComment: null,
        partyComment: null,
        createTime: null,
        updateTime: null,
        deleted: null,
        createBy: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.semeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学期";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const semeId = row.semeId || this.ids
      getSemester(semeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学期";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.semeId != null) {
            updateSemester(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSemester(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const semeIds = row.semeId || this.ids;
      this.$modal.confirm('是否确认删除学期编号为"' + semeIds + '"的数据项？').then(function() {
        return delSemester(semeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('psedu-base/semester/export', {
        ...this.queryParams
      }, `semester_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
